package com.setup.trac.controller.validator;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.setup.trac.pojo.Allegati;
import com.setup.trac.pojo.condition.AllegatiFinderByIdAllegato;
import com.setup.trac.service.SearchService;

public class VisualizzaAllegatoController extends AbstractController {

	
	private SearchService searchService ;
	
	private List<String> inLineDownloadContentType = new ArrayList<String>() ;
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String idAllegato = request.getParameter("id");
		Allegati[] allegatis = getSearchService().find(new Allegati[0], new AllegatiFinderByIdAllegato(idAllegato), Allegati.class);
		byte[] b = allegatis[0].getAllegato();
		
		OutputStream out = response.getOutputStream();
		boolean inLineAttachment = getInLineDownloadContentType().contains(allegatis[0].getContentType()); 
		response.setContentType(allegatis[0].getContentType());
		response.setContentLength(b.length);
		response.setHeader("Content-Disposition", (inLineAttachment?"inline;":"attachment")+"; filename="+allegatis[0].getNome());
		out.write(b);
		out.flush();
		return null;
	}
	public SearchService getSearchService() {
		return searchService;
	}
	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}
	public List<String> getInLineDownloadContentType() {
		return inLineDownloadContentType;
	}
	public void setInLineDownloadContentType(List<String> inLineDownloadContentType) {
		this.inLineDownloadContentType = inLineDownloadContentType;
	}

}
